package com.course.server.service;

import cn.hutool.core.util.StrUtil;
import com.course.server.dto.CategoryDto;
import com.course.server.dto.PageDto;
import com.course.server.entity.Category;
import com.course.server.entity.CategoryExample;
import com.course.server.mapper.CategoryMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: Durian
 * @date: 2021/4/18 18:26
 */
@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    public PageDto<CategoryDto> list(int page, int size) {
        PageHelper.startPage(page, size);
        CategoryExample example = new CategoryExample();
        // criteria只能一次
        example.setOrderByClause("sort asc");
        List<Category> categories = categoryMapper.selectByExample(example);
        PageInfo<Category> pageInfo = new PageInfo<>(categories);

        List<CategoryDto> categoryDtos = CopyUtil.copyList(categories, CategoryDto.class);
        PageDto<CategoryDto> pageDto = new PageDto<>();
        pageDto.setPage(page);
        pageDto.setSize(size);
        pageDto.setList(categoryDtos);
        pageDto.setTotal(pageInfo.getTotal());
        return pageDto;
    }

    public List<CategoryDto> listAll() {
        CategoryExample example = new CategoryExample();
        // criteria只能一次
        example.setOrderByClause("sort asc");
        List<Category> categories = categoryMapper.selectByExample(example);
        List<CategoryDto> categoryDtos = new ArrayList<>();
        for (Category category : categories) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtils.copyProperties(category, categoryDto);
            categoryDtos.add(categoryDto);
        }
        return categoryDtos;
    }

    public void save(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        if (StrUtil.isBlank(categoryDto.getId())) {
            insert(category);
        } else {
            update(category);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    public void delete(String id) {
        deleteChildren(id);
        categoryMapper.deleteByPrimaryKey(id);
    }

    private void insert(Category category) {
        category.setId(UuidUtils.getShortUuid());
        categoryMapper.insert(category);
    }

    private void update(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    private void deleteChildren(String id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (Objects.equals("00000000", category.getParent())) {
            // 一级分类，删除子分类
            CategoryExample example = new CategoryExample();
            example.createCriteria().andParentEqualTo(category.getId());
            categoryMapper.deleteByExample(example);
        }
    }
}
